package org.csu.cms.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;

import org.csu.cms.dao.AccountMapper;
import org.csu.cms.dao.UserMapper;
import org.csu.cms.domain.Account;
import org.csu.cms.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

@Component("SupplierService")
public class SupplierService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private AccountMapper accountMapper;


    /** 取得数据库中所有用户信息 */
    public List<Account> getAllAccount(){
        List<Account> accountList = accountMapper.selectList(null);
        //Iterator<Account> accountIterator = accountList.iterator();
        return accountList;
    }


    /** 通过id获取account */
    public Account getAccountById(Long id){
        QueryWrapper<Account> accountQueryWrapper = new QueryWrapper<>();
        accountQueryWrapper.eq("id", id);
        Account account = accountMapper.selectOne(accountQueryWrapper);
        return account;
    }

    /** 通过id返回user */
    public User getUserById(Long id){
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("id", id);
        User user = userMapper.selectOne(userQueryWrapper);
        return user;
    }

    /** 修改用户密码 */
    public String updatePassword(Account account,String password, String new_password){
        UpdateWrapper<User> userUpdateWrapper = new UpdateWrapper<>();
        userUpdateWrapper.eq("id", account.getId());
        User user = userMapper.selectOne(userUpdateWrapper);
        if(user.getPassword().equals(password)){
            user.setPassword(new_password);
            userMapper.update(user, userUpdateWrapper);
            return "ok";
        }else{
            return "原密码错误";
        }
    }

    /** 将密码重置为123456 */
    public String resetPassword(Account account){
        UpdateWrapper<User> userUpdateWrapper = new UpdateWrapper<>();
        userUpdateWrapper.eq("id", account.getId());
        User user = userMapper.selectOne(userUpdateWrapper);
        if(user != null){
            user.setPassword("123456");
            userMapper.update(user, userUpdateWrapper);
            return "ok";
        }else{
            return "重置失败";
        }
    }



    /** 修改用户信息 */
    public Account updateAccount(Long id, Account updateAccount){
        UpdateWrapper<Account> accountUpdateWrapper = new UpdateWrapper<>();
        accountUpdateWrapper.eq("id", id);
        Account account = accountMapper.selectOne(accountUpdateWrapper);

        if(account != null){
            // 修改signon中用户名
            UpdateWrapper<User> userUpdateWrapper = new UpdateWrapper<>();
            userUpdateWrapper.eq("id", id);
            User user = userMapper.selectOne(userUpdateWrapper);
            user.setUsername(updateAccount.getUserid());

            accountMapper.update(updateAccount, accountUpdateWrapper);
            return updateAccount;
        }

        return null;
    }

    /**
     * 删除账户（account、signon表级联删除）
     *
     * @param id id
     */
    public void deleteAccount(Long id){
        QueryWrapper<Account> accountQueryWrapper = new QueryWrapper<>();
        accountQueryWrapper.eq("id", id);

        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("id", id);

        accountMapper.delete(accountQueryWrapper);
        userMapper.delete(userQueryWrapper);

    }
}
